import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class RpcServer {
    public static int corePoolSize = 5;
    public static int maximumPoolSize = 50;
    public static long keepAliveTime = 60;
    public final ExecutorService threadPool;

    public RpcServer(){
        BlockingQueue<Runnable> workingQueue = new ArrayBlockingQueue<>(100);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();

        threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,workingQueue, threadFactory);
    }

    public void register(Object service, int port){
        try(ServerSocket socket = new ServerSocket(port)){
            Socket cli;

            while ((cli = socket.accept()) != null){
                System.out.println("客户端已连接，ip为:  "+cli.getInetAddress());
                threadPool.execute(new WorkThread(cli, service));
                System.out.println("已将请求放入线程池，准备处理请求");
            }
        }catch (Exception e){

        }
    }
}
